/*
 * network_thread_handler.c
 *
 *  Created on: 2009-4-15
 *      Author: tian
 */
#include"tcpipsim.h"
#include<string.h>
#include<stdio.h>


int network_thread_handler(int msgqid){
	msg data;
	int rv;
	while(1){
		if((rv=msgrcv(msgqid,&data,sizeof(msg)-sizeof(long),0,0))<0){
			perror("Error in msgrcv");
			return -1;
		}
		if(pthread_mutex_lock(&mutex)!=0){
			fprintf(stderr,"mutex lock error in network thread\n");
		}
		switch(data.mtype){
		case MSG_IP_TUNNEL:
			ip_input(data.buf);
			break;
		case MSG_USR_SOCK:
			handle_socket(&data);
			break;
		case MSG_USR_READ:
			handle_read(&data);
			break;
		case MSG_USR_WRITE:
			handle_write(&data);
			break;
		case MSG_USR_CLOSE:
			handle_close(&data);
			break;
		case MSG_USR_BIND:
			handle_bind(&data);
			break;
		case MSG_USR_LISTEN:
			handle_listen(&data);
			break;
#ifndef EASY_ACCEPT
		case MSG_USR_ACCEPT:
			handle_accept(&data);
			break;
#endif
		case MSG_USR_CONNECT:
			handle_connect(&data);
			break;
		default:
			break;
		}
		if(pthread_mutex_unlock(&mutex)!=0){
			fprintf(stderr,"mutex unlock error in network thread\n");
		}
	}

}
